package com.buddies.lottery.ui.lottery.bet.period;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.buddies.lottery.R;
import com.buddies.lottery.base.list.BaseListActivity;
import com.buddies.lottery.data.bean.Lottery;
import com.buddies.lottery.data.bean.LotteryBetRecord;
import com.buddies.lottery.ui.lottery.bet.LotteryBetRecordActivity;
import com.buddies.lottery.ui.lottery.bet.LotteryBetRecordAdapter;
import com.buddies.lottery.ui.lottery.bet.detail.LotteryBetDetailActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.HashMap;
import java.util.Map;

public class LotteryPeriodBetRecordActivity extends BaseListActivity<LotteryBetRecord, LotteryPeriodBetRecordContract.Presenter> implements LotteryPeriodBetRecordContract.View {

    private Lottery mLottery;
    private String mPeriod;

    @Override
    protected void initView() {
        super.initView();

        TextView btnAllBetRecord = findViewById(R.id.tv_action);
        btnAllBetRecord.setText("投注记录");
        btnAllBetRecord.setVisibility(View.VISIBLE);
        btnAllBetRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LotteryBetRecordActivity.launch(getActivity(), mLottery);
            }
        });
    }

    @Override
    protected String getHeaderTitle() {
        return "当期投注";
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            mLottery = (Lottery) intent.getSerializableExtra("lottery");
            mPeriod = intent.getStringExtra("period");
        }

        if (mLottery == null || mLottery.id == 0 || TextUtils.isEmpty(mPeriod)) {
            finish();
            return;
        }

        super.initData();

        mPresenter = new LotteryPeriodBetRecordPresenter(this);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                LotteryBetRecord betRecord = (LotteryBetRecord) adapter.getItem(position);
                LotteryBetDetailActivity.launch(getActivity(), betRecord);
            }
        });
    }

    @Override
    protected BaseQuickAdapter createAdapter() {
        return new LotteryBetRecordAdapter(null);
    }

    @Override
    public void onLoadData(int page) {
        Map<String, Object> params = new HashMap<>();
        params.put("lotteryId", mLottery.id);
        params.put("period", mPeriod);
        params.put("status", "");

        ((LotteryPeriodBetRecordContract.Presenter) mPresenter).onLoadData(params, page);
    }

    public static void launch(Context context, Lottery lottery, String period) {
        Intent intent = new Intent();
        intent.putExtra("lottery", lottery);
        intent.putExtra("period", period);
        intent.setClass(context, LotteryPeriodBetRecordActivity.class);
        context.startActivity(intent);
    }

}
